<?php

class Dailynews extends Controller {

	function Dailynews()
	{
		parent::Controller();	
		$this->load->scaffolding('dailynews');
		$this->load->library("dailynews_lib");
	}
	

	
	function __add($title,$body)
	{
		$this->load->model("dailynews_model");
		//$title =  "手机报-新闻早报5.5";
 		preg_match('@新闻.*(\d+)\.(\d+)$@i',$title, $matches);
 		if(count($matches)==3)
 		{
 			$date = date("Y")."-{$matches[1]}-{$matches[2]} 0:0:0";
			if($title!="" and $body!="")
			{
				$ret = $this->dailynews_model->getInfo(array("title"=>$title));
				if($ret->id==0)
				{
					$rec = new stdClass();
					$rec->title = $title ;
					$rec->body = $body;
					$rec->dateline = time();	
					if(strpos($title,"早")===false)	
					$rec->newsdate = strtotime($date)+72000;
					else
					$rec->newsdate = strtotime($date)+32400;
					$return = $this->dailynews_model->insert($rec);
					return $return;
				}
			} 			
 		}else{
 			return 0;
 		}



		return 0;
	}
	
	function add()
	{
 		$this->load->view('dailynews_add');

	}
	
	function do_add()
	{
		$title = trim($this->input->post("title"));
		$body = trim($this->input->post("body"));
		$ret = array("succ"=>0);
		if($this->__add($title,$body)>0)
		{
			$ret['succ'] =  1;
		}
		file_get_contents($this->config->item("base_url")."/index.php/wordpress/sync");
		echo json_encode($ret);
	}
	
	function check_exist()
	{
		$title = trim($this->input->post("title"));
		$ret = array("succ"=>0);
		if($title!="")
		{
			$this->load->model("dailynews_model");
			$info = $this->dailynews_model->getInfo(array("title"=>$title));
			if($info->id>0)
			{
				$ret['succ'] =  1;
			}
		}	
		echo json_encode($ret);
	}
		
	function getone($id)
	{
		$this->load->model("dailynews_model");
	}
	
	function getlist($lastdate=0)
	{

		$this->load->model("dailynews_model");
		//$lastdate = strtotime(trim($this->input->post("lastdate")));
		//if($lastdate=="") $lastdate=0;
		$ret = array();
		$ret['succ'] = 1;
		
		$nlist = $this->dailynews_model->getList(" newsdate>$lastdate ","newsdate desc",1,3);
		foreach($nlist as $row)
		{
			$temp = array();
			$temp["id"] = $row->id;
			$temp["title"] = $this->dailynews_lib->filtertitle($row->title);
			$temp["body"] = $this->dailynews_lib->filterbody($row->body);
			$temp["newsdate"] = $row->newsdate;
			$temp["dateline"] = $row->dateline;
			$ret['contents'][]=$temp;
		}
		echo json_encode($ret);
		
	}
	
	function index()
	{
		$this->load->model("dailynews_model");
		$this->getlist();
		
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */